$(document).ready(function() {
	// 填充浏览统计数据
	$.ajax({
		url: "http://localhost:8081/user/list",
		type: "GET",
		dataType: "json",
		success: function(json) {
			$.each(json, function(i, item) {
				$('#tbody-users').append(
					'<tr><td>' + +item.userId +
					'</td><td>' + item.userName +
					'</td><td>' + item.userAge +
                    '</td><td>' + item.userEmail +
                    '</td><td>' + item.userTelephoneNumber +
                    '</td><td>' + item.role.roleName +
                    '</td><td>' + item.userRegistrationTime +
					'</td><td><button class="btn btn-success" onclick="updateUser(' + item.userId + ')"><i class="fa fa-edit"></i> 编辑</button> ' +
                    '<button class="btn btn-danger deleteBtn" onclick="deleteUser(' + item.userId + ')"><i class="fa fa-trash-o"></i>删除</button></td></tr>');
			});
			$('#dataTables-users').dataTable({

			});
		}
	});
});

// 删除按钮点击事件
function deleteUser(id) {
	$('#confirmBtn').attr("userId", id);
	$('#myModal').modal();
};

// 确认删除按钮点击事件
$('#confirmBtn').click(function() {
	var id = $(this).attr("userId");
	$.ajax({
		type: "DELETE",
		url: "http://localhost:8081/admin/user/" + id,
		success: function(result) {
			// 刷新页面
            toastr.success(result.message);
            setTimeout(function() {
                window.location.reload();
            }, 2000)
		}
	});
});

// 增加用户按钮点击事件
$('#addUserBtn').click(function() {
	var name = $('#addName').val();
	var password = $('#addPassword').val();
    var age = $('#addAge').val();
    var email = $('#addEmail').val();
    var phone = $('#addTelephone').val();
    var role = $('#addRole').val();
    var introduction =$('#addIntroduction').val();
    if(!(/^1[3456789]\d{9}$/.test(phone))){
        toastr.success("手机号格式有误，请重新填写");
        return false;
    }
    var reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
    if(!reg.test(email)){
        toastr.success("邮箱格式不正确，请重新输入");
        return false;
    }

	var json = {
		userName: name,
        userPassword: password,
        userAge: age,
        userEmail: email,
        userTelephoneNumber: phone,
		userIntroduction: introduction,
        userRole: role
	};
	$.ajax({
		type: "POST",
		dataType: "json",
		contentType: "application/json;charset=utf-8",
		url: "http://localhost:8081/admin/user",
		data: JSON.stringify(json),
		success: function(result) {
			// 刷新页面
            toastr.success(result.message);
            setTimeout(function() {
                window.location.reload();
            }, 2000)
		},
		error: function() {
			location.reload();
		}
	});
});

//显示添加用户框
function addUser() {
    // 显示模态框
    $('#addModal').modal();
}

// 编辑用户按钮点击
function updateUser(userId) {
    // 往模态框填充数据
    $('#updateBtn').attr("userId", userId);
    $.ajax({
        type: "get",
        url: "http://localhost:8081/admin/user/" + userId,
        dataType: "json",
        async: false,
        success: function (json) {
            $('#name').val(json.userName);
            // $('#password').val(json.userPassword);
            $('#age').val(json.userAge);
            $('#email').val(json.userEmail);
            $('#phone').val(json.userTelephoneNumber);
            $('#introduction').val(json.userIntroduction);
            $('#role').val(json.userRole);
        }
    });

    // 显示模态框
    $('#updateModal').modal();
}

// 更新用户按钮点击事件
$('#updateBtn').click(function () {
    var id = $('#updateBtn').attr("userId");
    var name = $('#name').val();
    var age = $("#age").val();
    var email = $("#email").val();
    var phone = $("#phone").val();
    var introduction = $("#introduction").val();
    var role = $("#role").val();
    if((phone.valueOf().length > 0) && !(/^1[3456789]\d{9}$/.test(phone))){
        toastr.success("手机号格式有误，请重新填写");
        return false;
    }
    var reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
    if((email.valueOf().length > 0) && !reg.test(email)){
        toastr.success("邮箱格式不正确，请重新输入");
        return false;
    }
    var user = {
        userId: id,
        userName: name,
		userAge: age,
		userEmail: email,
		userTelephoneNumber: phone,
		userIntroduction: introduction,
		userRole: role
    };
    $.ajax({
        type: "PUT",
        url: "http://localhost:8081/admin/user/" + id,
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify(user),
        success: function (result) {
            toastr.success(result.message);
            setTimeout(function() {
                window.location.reload();
            }, 2000)
        },
        error: function () {
            location.reload();
        }
    });
});